rm(list = ls())

library(data.table)
library(dplyr)
library(ggplot2)


data <- readRDS('./replication_hasz/output/data/data_pooled.rds')
data$study <- factor(data$study, levels=c('Pooled','Florida Study','U.S. Study'))
data <- data[democrat==1 | republican==1]

## vote_ownc
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(vote_ownc, na.rm = TRUE),
    sd=sd(vote_ownc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in own local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  theme_bw(base_size = 12)
ggsave('./replication_hasz/output/figures/figD2b.pdf', width=6.5, height=5)


### vote_usc
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(vote_usc, na.rm = TRUE),
    sd=sd(vote_usc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in U.S. local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  ylab(NULL) +
  theme_bw(base_size = 12)
ggsave('./replication_hasz/output/figures/figD2a.pdf', width=6.5, height=5)


### ballot
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(ballotc, na.rm = TRUE),
    sd=sd(ballotc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(name = 'Support for noncitizen voting in STATE local elections', breaks = c(1,2,3,4,5),
                     labels = c(paste('Definitely', 'would not', sep='\n'),
                                paste('Probably', 'would not', sep='\n'),
                                paste('May or', 'may not', sep = '\n'),
                                paste('Probably', 'would', sep='\n'),
                                paste('Definitely', 'would', sep='\n')),
                     limits = c(0,5)) +
  theme_bw(base_size = 11) + theme(plot.title = element_text(size = 10), axis.text.x=element_text(size=10))
ggsave('./replication_hasz/output/figures/figD2c.pdf', width=6.5, height=5)


### more_info
my_sum <- data[] %>%
  group_by(treatment, study) %>%
  summarise( 
    n=n(),
    mean=mean(more_infoc, na.rm = TRUE),
    sd=sd(more_infoc, na.rm = TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

ggplot(my_sum, aes(x=treatment, y=mean)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-ic, ymax=mean+ic), width=0.2, position=position_dodge(.9),
                colour="black", alpha=0.9)  +
  facet_wrap(~study) +
  scale_x_continuous(name = 'Experimental condition', breaks = c(1,2,3), labels = c("control", paste("co","partisan", sep="\n"),
                                                                paste("counter","partisan", sep="\n"))) +
  scale_y_continuous(name = 'Willingness to learn about noncitizen voting', breaks = c(0,1),
                     labels = c('No', 'Yes'),
                     limits = c(0,1)) +
  ylab(NULL) +
  theme_bw(base_size = 12)
ggsave('./replication_hasz/output/figures/figD2d.pdf', width=6.5, height=5)
